package com.ruoyi.platform.service.impl;

import java.util.List;
import java.util.Date;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.platform.mapper.MarketingActivityMapper;
import com.ruoyi.platform.domain.MarketingActivity;
import com.ruoyi.platform.service.IMarketingActivityService;

/**
 * 营销活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class MarketingActivityServiceImpl implements IMarketingActivityService 
{
    @Autowired
    private MarketingActivityMapper marketingActivityMapper;

    /**
     * 查询营销活动
     * 
     * @param activityId 营销活动主键
     * @return 营销活动
     */
    @Override
    public MarketingActivity selectMarketingActivityByActivityId(String activityId)
    {
        return marketingActivityMapper.selectMarketingActivityByActivityId(activityId);
    }

    /**
     * 查询营销活动列表
     * 
     * @param marketingActivity 营销活动
     * @return 营销活动
     */
    @Override
    public List<MarketingActivity> selectMarketingActivityList(MarketingActivity marketingActivity)
    {
        return marketingActivityMapper.selectMarketingActivityList(marketingActivity);
    }

    /**
     * 新增营销活动
     * 
     * @param marketingActivity 营销活动
     * @return 结果
     */
    @Override
    public int insertMarketingActivity(MarketingActivity marketingActivity)
    {
        // 生成活动ID
        if (StringUtils.isEmpty(marketingActivity.getActivityId())) {
            marketingActivity.setActivityId(IdUtils.fastSimpleUUID());
        }
        
        // 生成活动编码
        if (StringUtils.isEmpty(marketingActivity.getActivityCode())) {
            marketingActivity.setActivityCode("ACT" + System.currentTimeMillis());
        }
        
        // 设置默认状态
        if (marketingActivity.getAuditStatus() == null) {
            marketingActivity.setAuditStatus(0); // 待审核
        }
        if (marketingActivity.getStatus() == null) {
            marketingActivity.setStatus(0); // 待审核
        }
        
        // 设置创建信息
        marketingActivity.setCreateBy(SecurityUtils.getUsername());
        marketingActivity.setCreateTime(DateUtils.getNowDate());
        
        return marketingActivityMapper.insertMarketingActivity(marketingActivity);
    }

    /**
     * 修改营销活动
     * 
     * @param marketingActivity 营销活动
     * @return 结果
     */
    @Override
    public int updateMarketingActivity(MarketingActivity marketingActivity)
    {
        marketingActivity.setUpdateBy(SecurityUtils.getUsername());
        marketingActivity.setUpdateTime(DateUtils.getNowDate());
        return marketingActivityMapper.updateMarketingActivity(marketingActivity);
    }

    /**
     * 批量删除营销活动
     * 
     * @param activityIds 需要删除的营销活动主键
     * @return 结果
     */
    @Override
    public int deleteMarketingActivityByActivityIds(String[] activityIds)
    {
        return marketingActivityMapper.deleteMarketingActivityByActivityIds(activityIds);
    }

    /**
     * 删除营销活动信息
     * 
     * @param activityId 营销活动主键
     * @return 结果
     */
    @Override
    public int deleteMarketingActivityByActivityId(String activityId)
    {
        return marketingActivityMapper.deleteMarketingActivityByActivityId(activityId);
    }

    /**
     * 根据商家ID查询营销活动列表
     * 
     * @param merchantId 商家ID
     * @return 营销活动集合
     */
    @Override
    public List<MarketingActivity> selectMarketingActivityByMerchantId(String merchantId)
    {
        return marketingActivityMapper.selectMarketingActivityByMerchantId(merchantId);
    }

    /**
     * 根据审核状态查询营销活动列表
     * 
     * @param auditStatus 审核状态
     * @return 营销活动集合
     */
    @Override
    public List<MarketingActivity> selectMarketingActivityByAuditStatus(Integer auditStatus)
    {
        return marketingActivityMapper.selectMarketingActivityByAuditStatus(auditStatus);
    }

    /**
     * 根据商家ID和审核状态查询营销活动列表
     * 
     * @param merchantId 商家ID
     * @param auditStatus 审核状态
     * @return 营销活动集合
     */
    @Override
    public List<MarketingActivity> selectMarketingActivityByMerchantIdAndAuditStatus(String merchantId, Integer auditStatus)
    {
        return marketingActivityMapper.selectMarketingActivityByMerchantIdAndAuditStatus(merchantId, auditStatus);
    }

    /**
     * 审核通过营销活动
     * 
     * @param activityId 活动ID
     * @param auditRemark 审核备注
     * @return 结果
     */
    @Override
    public int approveMarketingActivity(String activityId, String auditRemark)
    {
        MarketingActivity activity = new MarketingActivity();
        activity.setActivityId(activityId);
        activity.setAuditStatus(1); // 审核通过
        activity.setStatus(3); // 已发布
        activity.setAuditBy(SecurityUtils.getUserId());
        activity.setAuditAt(new Date());
        activity.setAuditRemark(auditRemark);
        activity.setUpdateBy(SecurityUtils.getUsername());
        activity.setUpdateTime(DateUtils.getNowDate());
        
        return marketingActivityMapper.updateMarketingActivityAuditStatus(activity);
    }

    /**
     * 审核拒绝营销活动
     * 
     * @param activityId 活动ID
     * @param rejectReason 拒绝原因
     * @return 结果
     */
    @Override
    public int rejectMarketingActivity(String activityId, String rejectReason)
    {
        MarketingActivity activity = new MarketingActivity();
        activity.setActivityId(activityId);
        activity.setAuditStatus(2); // 审核拒绝
        activity.setStatus(2); // 审核拒绝
        activity.setAuditBy(SecurityUtils.getUserId());
        activity.setAuditAt(new Date());
        activity.setRejectReason(rejectReason);
        activity.setUpdateBy(SecurityUtils.getUsername());
        activity.setUpdateTime(DateUtils.getNowDate());
        
        return marketingActivityMapper.updateMarketingActivityAuditStatus(activity);
    }

    /**
     * 检查活动编码是否唯一
     * 
     * @param activityCode 活动编码
     * @return 结果
     */
    @Override
    public String checkActivityCodeUnique(String activityCode)
    {
        MarketingActivity activity = marketingActivityMapper.checkActivityCodeUnique(activityCode);
        if (StringUtils.isNotNull(activity))
        {
            return "1"; // 不唯一
        }
        return "0"; // 唯一
    }

    /**
     * 导入营销活动数据
     * 
     * @param activityList 营销活动数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importMarketingActivity(List<MarketingActivity> activityList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(activityList) || activityList.size() == 0)
        {
            throw new ServiceException("导入营销活动数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MarketingActivity activity : activityList)
        {
            try
            {
                // 验证是否存在这个活动
                MarketingActivity a = marketingActivityMapper.checkActivityCodeUnique(activity.getActivityCode());
                if (StringUtils.isNull(a))
                {
                    activity.setCreateBy(operName);
                    this.insertMarketingActivity(activity);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、活动 " + activity.getActivityName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    activity.setActivityId(a.getActivityId());
                    activity.setUpdateBy(operName);
                    this.updateMarketingActivity(activity);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、活动 " + activity.getActivityName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、活动 " + activity.getActivityName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、活动 " + activity.getActivityName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}